import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * Creater with IntelliJ IDEA.
 * Description:
 * User: 86152
 * Dae: 2022-08-04
 * Time: 19:05
 */
public class demo2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你要搜索的目录：");
        String rootPath = scanner.next();
        File root = new File(rootPath);
        if(!root.exists()) {
            System.out.println("你输入的不是文件不存在");
            return;
        }
        if(!root.isDirectory()) {
            System.out.println("你输入的不是目录，退出");
            return;
        }
        System.out.print("请输入你要搜索的关键字：");
        String key = scanner.next();
        ArrayList<File> list = new ArrayList<File>();
        Scan(root,key,list);
        System.out.println("找到以下文件");
        for(File curFile:list) {
            System.out.println(curFile.getName());
            System.out.println(curFile.getAbsoluteFile());
        }
    }
    public static void Scan(File root,String key,ArrayList<File> list) {
        File[] fileList= root.listFiles();
        for (File curFile:fileList) {
            if(curFile.isDirectory()) {
                Scan(curFile,key,list);
            } else {
                if(curFile.getName().contains(key)) {
                    list.add(curFile);
                }
            }
        }
    }
}
